package com.ruoyi.HKISAPI.controller;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DecimalFormat;


public class DirectorySizeCalculator {

    public static void main(String[] args) {
        Path directoryPath = Paths.get("/Users/wangxiaoxing/Desktop/sql/2024_7/5/15_17_45_705_fielddetection.jpg"); // 替换为你的目录路径
        Path directoryPath2 = Paths.get("/Users/wangxiaoxing/Desktop"); // 替换为你的目录路径

        try {
            long sizeInBytes = Files.walk(directoryPath)
                    .filter(Files::isRegularFile)
                    .mapToLong(path -> {
                        try {
                            return Files.size(path);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    })
                    .sum();

            long sizeInBytes2 = Files.walk(directoryPath2)
                    .filter(Files::isRegularFile)
                    .mapToLong(path -> {
                        try {
                            return Files.size(path);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    })
                    .sum();

            double sizeInGB = sizeInBytes / 1024.0 / 1024 / 1024;
            double sizeInGB2 = sizeInBytes2 / 1024.0 / 1024 / 1024;

            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println("Total size of directory content: " + df.format(sizeInGB) + " GB");
            System.out.println("Total size of directory content: " + df.format(sizeInGB2) + " GB");

        } catch (IOException e) {
            System.err.println("Error reading directory: " + e.getMessage());
        }
    }

}
